<template>
  <div class="goods-relevant">
    <div class="header">
      <i class="icon" />
      <span class="title">同类商品推荐</span>
    </div>
    <!-- 此处放改造后的xtx-carousel -->
    <XtxCarousel :sliders="sliders" style="height: 380px" auto-play />
  </div>
</template>

<script>
import { findRelGoods } from '@/api/product'
import { ref } from 'vue'
export default {
  name: 'GoodsRelevant',
  props: {
    goodsId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    // 最终获取的数据是 sliders 提供提供轮播图使用
    // 数据结构: sliders = [[4个],[4个],[4个],[4个]]
    const sliders = ref([])
    findRelGoods({ id: props.goodsId }).then(data => {
      // data.result 商品列表,数据结构 [16个]
      const pageSize = 4
      const pageList = Math.ceil(data.result.length / pageSize)
      for (let i = 0; i < pageList; i++) {
        sliders.value.push(data.result.slice(pageSize * i, pageSize * (i + 1)))
      }
    })
    return { sliders }
  }
}
</script>

<style scoped lang="less">
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
  .header {
    height: 80xp;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(@xtxColor, 40%);
      }
    }
  }
}
</style>
